OPTIONAL Sprite-animation.f

Requires FixedPoint
Requires SORA/Spritesets
Requires Dorothy

Only Forth Definitions

Type: sprctl
   fixed.field counter
   int.field frame#
   int.field sprite#
   subimage.pointer subimage
   spriteset.pointer spriteset
   fixed.field speed
end-type

: sprctl.frame-addr  ( sprctl -- frame )
   dup frame#.@ swap spriteset.frames.[] ;

method: sprctl.animstep  ( -- )
   speed.@ if
      counter.@  speed.@  -  dup counter !  dup  0 <= if
         frame#.@  spriteset.next-frame  dup -1 > if frame# !
            frame-addr  subimage !
            drop
            subimage.delay.@  s>.  counter +!
         else  drop then
      else
         drop
      then
   then ;

method: sprctl.setframe   ( frame# -- )
   swap over frame#.!
   dup frame-addr  swap subimage.! ;

method: sprctl.setsprite  ( sprite# -- )
   sprite#.!
   sprite#.@  spriteset.first-frame  frame#.!
   frame-addr  subimage.!
   subimage.delay.@  s>.  counter.! ;
